package org.example.demospring.controller;

import org.example.demospring.entity.User;
import org.example.demospring.mapper.UserMapper;
import org.example.demospring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    UserService userService;

//    全部查询
    @GetMapping("inquire")
    public List<User> inquire() {
        List<User> all=userMapper.findAll();
        return all;
    }

//    分页查询
    @GetMapping("/page")
    public Map<String,Object> page(@RequestParam int page, @RequestParam int size,@RequestParam String user_name) {
        page=(page-1)*size;
        user_name="%"+user_name+"%";
        List<User> data=userMapper.selectPage(page,size,user_name);
        Integer total=userMapper.selectTotal(user_name);
        Map<String,Object> res=new HashMap();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

//    更新
    @PostMapping("/save")
    public Integer save(@RequestBody User user) {
        return userService.save(user);
    }

//    删除
    @DeleteMapping("delete/{id}")
    public Integer deleteUser(@PathVariable Integer id) {
        return userMapper.delete(id);
    }
}